package com.ruoyi.modules.mq;


import com.ruoyi.crm.domain.Contract;
import com.ruoyi.modules.email.CrmMailService;
import com.ruoyi.modules.sms.CrmSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 收消息的
 */

@Component
@Slf4j
public class CrmMqConsumer {
    @Resource
    private CrmMailService crmMailService;
    @Resource
    private CrmSmsService crmSmsService;
    /**
     * 处理合同创建消息，发送邮件和短信通知给用户
     */
    @RabbitListener(queues = {CrmRabbitConfig.CRM_QUEUE_CONTRACT_CREATED})
    public void consumeContractCreated(Contract contract){
        //发送邮件
        if(StringUtils.hasText(contract.getEmail())){
            crmMailService.sendContractCreateMail(contract);
        }
        //发送短信
        if(StringUtils.hasText(contract.getPhone())){
            try{
                crmSmsService.sendMessage(contract.getPhone(),"短信内容","1",new String[] {contract.getName(),contract.getContractOrder()+""});
            }catch (Throwable throwable){
                log.error(throwable.getMessage(),throwable);
            }
        }
    }


}
